<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-date-picker v-model="listQuery.date" type="monthrange" align="right" unlink-panels range-separator="至"
        start-placeholder="开始月份" end-placeholder="结束月份" :picker-options="pickerOptions">
      </el-date-picker> -->
      <el-date-picker v-model="listQuery.start" style="width:150px;" format="yyyy-MM-dd" type="datetime" placeholder="选择开始日期" />
      <el-date-picker v-model="listQuery.end" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="选择结束日期" />

      <el-input v-model="listQuery.merchant" placeholder="医院名称" style="width: 200px;margin-top:9px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:9px" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px" @click="excelFen">导出</el-button>

    </div>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">

      <el-table-column label="医院名称" align="center" width="400">
        <template slot-scope="{row}">
          <span>{{ row.merchant_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="结算时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.settle_day }}</span>
        </template>
      </el-table-column>

      <el-table-column label="应付金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.should_pay_money }}</span>
        </template>
      </el-table-column>

      <el-table-column label="已付金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.paid_money }}</span>
        </template>
      </el-table-column>

      <el-table-column label="未付金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.no_pay_money }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button size="mini" type="warning" @click="jumpDetail(row.settle_day,row.merchant_id,row.account_type)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog title="详情" :visible.sync="infodetail">
      <span><el-button type="primary" @click="excelout">导出</el-button></span>
      <el-table :key="tableKey" v-loading="listLoading" :data="infolist" border fit highlight-current-row style="width: 1500px">
        <el-table-column label="医院名称" align="center" width="360px">
          <template slot-scope="{row}">
            <span>{{ row.merchant_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.real_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="当前期数" align="center">
          <template slot-scope="{row}">
            <span>{{ row.qs?row.qs:row.new_num }}</span>
          </template>
        </el-table-column>

        <el-table-column label="结算时间" align="center">
          <template slot-scope="{row}">
            <span>{{ row.settle_day }}</span>
          </template>
        </el-table-column>

        <el-table-column label="应付金额" align="center">
          <template slot-scope="{row}">
            <span>{{ row.should_pay_money }}</span>
          </template>
        </el-table-column>

        <el-table-column label="已付金额" align="center">
          <template slot-scope="{row}">
            <span>{{ row.settle_money }}</span>
          </template>
        </el-table-column>

        <el-table-column label="未付金额" align="center">
          <template slot-scope="{row}">
            <span>{{ row.no_pay_money }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>

</template>

<script>
import {
  getOrderPaymentHandDayList, merchantDayPayment, merchantdayexcelout,excelFen
} from '@/api/finance'
import waves from '@/directive/waves' // waves directive
import {
  formatDate
} from '@/utils/formatDate.js'
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ReceiveTable',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {

      tableKey: 0,
      list: null,
      infodetail: false,
      total: 0,
      listLoading: true,
      infolist: [],
      listQuery: {
        page: 1,
        limit: 10,
        start:'',
        end:''
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      infoQuery: {
        day: '',
        merchant_id: '',
        account_type: ''
      },
      value1: '',
      value2: '',
      value3: ''
    }
  },
  created() {
    // let dt=new Date();
    //  let start_time=new Date().setMonth(new Date().getMonth())
    //  let end_time=new Date().setMonth(new Date().getMonth()+4)
    //  this.listQuery.date.push(new Date(start_time).getFullYear()+"-"+new Date(start_time).getMonth())
    //  this.listQuery.date.push(new Date(end_time).getFullYear()+"-"+new Date(end_time).getMonth())
    this.cacheQueryFormat()
    this.getList()
    // 添加钩子，页面刷新
    window.addEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  mdestroyed() {
    // 添加钩子，页面刷新
    window.removeEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  methods: {
    beforeunloadFn(e) {
      // 刷新页面事件
      sessionStorage.removeItem('finacefen')
    },
    cacheQueryFormat() {
      const cacheQuery = sessionStorage.getItem('finacefen')
      if (cacheQuery) {
        this.listQuery = JSON.parse(cacheQuery)
      }
    },
    jumpDetail(day, merchant_id, account_type) {
      this.infodetail = true
      this.infoQuery.day = day
      this.infoQuery.merchant_id = merchant_id
      this.infoQuery.account_type = account_type
      this.getInfo()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList() {
      sessionStorage.setItem('finacefen', JSON.stringify(this.listQuery))
      getOrderPaymentHandDayList(this.listQuery).then(res => {
        this.list = res.data.data
        this.total = res.data.total
      })
      this.listLoading = false
    },
    getInfo() {
      merchantDayPayment(this.infoQuery).then(res => {
        this.infolist = res.data
      })
      this.listLoading = false
    },
    excelout() {
      merchantdayexcelout(this.infoQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    excelFen() {
      excelFen(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    }
  }
}
</script>

<style>
</style>
